package pl.olap.gui;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.border.Border;

import pl.olap.dao.Movie;
import pl.olap.persistence.SearchUtil;

public class SearchPanel extends JPanel {

	/** serialization uid */
	private static final long serialVersionUID = 7561463374295070477L;

	private LeftPanel leftPanel = null;
	private static final int TITLE_SEARCH_MODE = 1;
	private static final int ACTOR_SEARCH_MODE = 2;
	private static final int HACTOR_SEARCH_MODE = 3;
	private static final int PRODUCTION_SEARCH_MODE = 4;
	private static final int RATE_SEARCH_MODE = 5;

	private int searchMode = TITLE_SEARCH_MODE;

	private JTextField searchField = new JTextField();
	private JButton searchButton = new JButton("Wyszukaj");

	public SearchPanel(LeftPanel l) {
		// ustawiam border
		Border b = BorderFactory.createLoweredBevelBorder();
		setBorder(b);
		this.leftPanel = l;
		Box searchBox = Box.createHorizontalBox();
		searchBox.add(searchField);
		searchBox.add(searchButton);

		JRadioButton titleButton = new JRadioButton("Wyszukiwanie po tytule");
		titleButton.setMnemonic(KeyEvent.VK_B);
		titleButton.setActionCommand("Wyszukiwanie po tytule");
		titleButton.setSelected(true);

		JRadioButton actorButton = new JRadioButton("Wyszukiwanie po aktorze");
		actorButton.setMnemonic(KeyEvent.VK_C);
		actorButton.setActionCommand("Wyszukiwanie po aktorze");

		JRadioButton hActorButton = new JRadioButton(
				"Z najwieksza iloscia aktorow");
		hActorButton.setMnemonic(KeyEvent.VK_D);
		hActorButton.setActionCommand("Z najwieksza iloscia aktorow");

		JRadioButton productionButton = new JRadioButton(
				"Wyszukiwanie po produkcji");
		productionButton.setMnemonic(KeyEvent.VK_R);
		productionButton.setActionCommand("Wyszukiwanie po produkcji");

		JRadioButton rateButton = new JRadioButton("Wyszukiwanie po ocenach");
		rateButton.setMnemonic(KeyEvent.VK_P);
		rateButton.setActionCommand("Wyszukiwanie po ocenach");

		// Group the radio buttons.
		ButtonGroup group = new ButtonGroup();
		group.add(titleButton);
		group.add(actorButton);
		group.add(hActorButton);
		group.add(productionButton);
		group.add(rateButton);
		JPanel buttonPanel = new JPanel();
		buttonPanel.setLayout(new GridLayout(1, 5));
		buttonPanel.add(titleButton);
		buttonPanel.add(actorButton);
		buttonPanel.add(hActorButton);
		buttonPanel.add(productionButton);
		buttonPanel.add(rateButton);
		Box vBox = Box.createVerticalBox();
		vBox.add(searchBox);
		vBox.add(buttonPanel);
		add(vBox, BorderLayout.CENTER);

		// Register a listener for the radio buttons.
		titleButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				searchMode = TITLE_SEARCH_MODE;
				searchField.setEnabled(true);
			}
		});
		actorButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				searchMode = ACTOR_SEARCH_MODE;
				searchField.setEnabled(true);
			}
		});
		hActorButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				searchMode = HACTOR_SEARCH_MODE;
				searchField.setEnabled(false);
			}
		});
		productionButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				searchMode = PRODUCTION_SEARCH_MODE;
				searchField.setEnabled(true);
			}
		});
		rateButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				searchMode = RATE_SEARCH_MODE;
				searchField.setEnabled(true);
			}
		});

		// action listener dla wyszukiwania
		searchButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				// akcja wyszukiwania w zaleznosci od typu
				List<Movie> movieList = new ArrayList<Movie>();
				switch (searchMode) {
				case TITLE_SEARCH_MODE:
					if (searchField.getText() != null) {
						movieList = SearchUtil.searchMovieByTitle(searchField
								.getText());
					}
					break;
				case ACTOR_SEARCH_MODE:
					if (searchField.getText() != null) {
						movieList = SearchUtil.searchMovieByActor(searchField
								.getText());
					}
					break;
				case HACTOR_SEARCH_MODE:
					break;
				case PRODUCTION_SEARCH_MODE:
					if (searchField.getText() != null) {
						movieList = SearchUtil
								.searchMovieByProduction(searchField.getText());
					}
					break;
				case RATE_SEARCH_MODE:
					if (searchField.getText() != null) {
						movieList = SearchUtil.searchMovieByRate(searchField
								.getText());
					}
					break;
				}

				boolean empty = movieList.isEmpty();
				createViewForMovies(movieList);
			}
		});

	}

	private void createViewForMovies(List<Movie> movieList) {
		leftPanel.setMovies(movieList);
	}

	/**
	 * @return the leftPanel
	 */
	public LeftPanel getLeftPanel() {
		return leftPanel;
	}

	/**
	 * @param leftPanel
	 *            the leftPanel to set
	 */
	public void setLeftPanel(LeftPanel leftPanel) {
		this.leftPanel = leftPanel;
	}

}
